﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraPrinting;
using DevExpress.XtraReports.UI;
using ComboBox = System.Windows.Forms.ComboBox;

namespace Dictionary.ERP
{
    public class DIC_SYMBOL
    {
        private string m_Description;
        private bool m_IsEdit;
        private bool m_IsShow;
        private double m_PercentSalary;
        private string m_SymbolCode;
        private string m_SymbolName;

        public DIC_SYMBOL()
        {
            m_SymbolCode = "";
            m_SymbolName = "";
            m_PercentSalary = 0.0;
            m_IsEdit = true;
            m_IsShow = true;
            m_Description = "";
        }

        public DIC_SYMBOL(string SymbolCode, string SymbolName, double PercentSalary, bool IsEdit, bool IsShow,
            string Description)
        {
            m_SymbolCode = SymbolCode;
            m_SymbolName = SymbolName;
            m_PercentSalary = PercentSalary;
            m_IsEdit = IsEdit;
            m_IsShow = IsShow;
            m_Description = Description;
        }

        [DisplayName("Description"), Category("Column")]
        public string Description
        {
            get { return m_Description; }
            set { m_Description = value; }
        }

        [Category("Column"), DisplayName("IsEdit")]
        public bool IsEdit
        {
            get { return m_IsEdit; }
            set { m_IsEdit = value; }
        }

        [Category("Column"), DisplayName("IsShow")]
        public bool IsShow
        {
            get { return m_IsShow; }
            set { m_IsShow = value; }
        }

        [Category("Column"), DisplayName("PercentSalary")]
        public double PercentSalary
        {
            get { return m_PercentSalary; }
            set { m_PercentSalary = value; }
        }

        public string ProductName
        {
            get { return "Class DIC_SYMBOL"; }
        }

        public string ProductVersion
        {
            get { return "1.0.0.0"; }
        }

        [DisplayName("SymbolCode"), Category("Primary Key")]
        public string SymbolCode
        {
            get { return m_SymbolCode; }
            set { m_SymbolCode = value; }
        }

        [Category("Column"), DisplayName("SymbolName")]
        public string SymbolName
        {
            get { return m_SymbolName; }
            set { m_SymbolName = value; }
        }

        public void AddCombo(ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "SymbolName", "SymbolCode");
        }

        public void AddComboAll(ComboBox combo)
        {
            var list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["SymbolCode"] = "All";
            row["SymbolName"] = "Tất cả";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "SymbolName", "SymbolCode");
        }

        public void AddComboEdit(ComboBoxEdit combo)
        {
            var list = new DataTable();
            list = GetList();
            foreach (DataRow row in list.Rows)
            {
                combo.Properties.Items.Add(row["SymbolName"]);
            }
        }

        public void AddGridLookupEdit(GridLookUpEdit gridlookup)
        {
            var list = new DataTable();
            list = GetList();
            gridlookup.Properties.DataSource = list;
            gridlookup.Properties.DisplayMember = "SymbolName";
            gridlookup.Properties.ValueMember = "SymbolCode";
        }

        public void AddOTRepositoryGridLookupEdit(RepositoryItemGridLookUpEdit gridlookup)
        {
            var list = new DataTable();
            list = GetList();
            list.Rows.Add(new object[] {"TCT", "Tăng Ca Trước Giờ L\x00e0m Việc", 0, false, true, ""});
            list.Rows.Add(new object[] {"TCS", "Tăng Ca Sau Giờ L\x00e0m Việc", 0, false, true, ""});
            gridlookup.DataSource = list;
            gridlookup.DisplayMember = "SymbolCode";
            gridlookup.ValueMember = "SymbolCode";
        }

        public void AddRepositoryGridLookupEdit(RepositoryItemGridLookUpEdit gridlookup)
        {
            var list = new DataTable();
            list = GetList();
            gridlookup.DataSource = list;
            gridlookup.DisplayMember = "SymbolCode";
            gridlookup.ValueMember = "SymbolCode";
        }

        public DataTable CreateNullDataTable()
        {
            var table = new DataTable();
            var column = new DataColumn("SymbolCode", typeof (string));
            var column2 = new DataColumn("SymbolName", typeof (string));
            var column3 = new DataColumn("PercentSalary", typeof (int));
            var column4 = new DataColumn("IsEdit", typeof (bool));
            var column5 = new DataColumn("Description", typeof (string));
            table.Columns.Add(column);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);
            table.Columns.Add(column5);
            table.Clear();
            return table;
        }

        public static void CreateReport(XtraReport Report, XRControl Band, bool IsShowLateEarly, bool IsShowShift)
        {
            string str;
            XRTable table;
            XRTableRow row2;
            XRTableCell cell;
            XRTableCell cell2;
            var child = new XRLabel
            {
                Text = "Bảng k\x00fd hiệu chấm c\x00f4ng",
                Font = new Font("Times New Roman", 9.25f, FontStyle.Italic | FontStyle.Bold),
                TopF = Band.TopF + 12f,
                WidthF = 200f,
                TextAlignment = TextAlignment.MiddleLeft
            };
            Band.Controls.Add(child);
            float num = 0f;
            float num2 = (child.TopF + child.HeightF) + 6f;
            var dic_symbol = new DIC_SYMBOL();
            foreach (DataRow row in dic_symbol.GetList().Rows)
            {
                if (bool.Parse(row["IsShow"].ToString()))
                {
                    str = "";
                    if (row["SymbolCode"].ToString() == "")
                    {
                        str = "(trống)";
                    }
                    else
                    {
                        str = row["SymbolCode"].ToString();
                    }
                    table = new XRTable();
                    row2 = new XRTableRow();
                    cell = new XRTableCell();
                    cell2 = new XRTableCell();
                    cell.WidthF = 102f;
                    cell.Text = row["SymbolName"].ToString();
                    cell.WordWrap = false;
                    cell.Font = new Font("Times New Roman", 8.75f);
                    cell.TextAlignment = TextAlignment.MiddleLeft;
                    cell2.WidthF = 62f;
                    cell2.Text = ": " + str;
                    cell2.WordWrap = false;
                    cell2.Font = new Font("Times New Roman", 8.75f);
                    cell2.TextAlignment = TextAlignment.MiddleLeft;
                    row2.Cells.Add(cell);
                    row2.Cells.Add(cell2);
                    table.Rows.Add(row2);
                    table.HeightF = 15f;
                    table.WidthF = 164f;
                    table.TopF = num2;
                    table.LeftF = num;
                    Band.Controls.Add(table);
                    if (((Report.PageWidth - (Report.Margins.Left + Report.Margins.Right)) - num) >= 328f)
                    {
                        num += 164f;
                    }
                    else
                    {
                        num = 0f;
                        num2 += 18f;
                    }
                }
            }
            if (IsShowLateEarly)
            {
                for (int i = 0; i < 4; i++)
                {
                    string str2 = "";
                    str = "";
                    switch (i)
                    {
                        case 0:
                            str2 = "Số ph\x00fat đi trễ: ";
                            str = ": +(số ph\x00fat)";
                            break;

                        case 1:
                            str2 = "Số ph\x00fat về sớm: ";
                            str = ": -(số ph\x00fat)";
                            break;

                        case 2:
                            str2 = "Đi trễ, về sớm: ";
                            str = ": +(sp):-(sp)";
                            break;

                        case 3:
                            str2 = "T\x00e1ch đ\x00f4i ca: ";
                            str = ": +;V +;P ...";
                            break;
                    }
                    table = new XRTable();
                    row2 = new XRTableRow();
                    cell = new XRTableCell();
                    cell2 = new XRTableCell();
                    cell.WidthF = 102f;
                    cell.Text = str2;
                    cell.WordWrap = false;
                    cell.Font = new Font("Times New Roman", 8.75f);
                    cell.TextAlignment = TextAlignment.MiddleLeft;
                    cell2.WidthF = 62f;
                    cell2.Text = str;
                    cell2.WordWrap = false;
                    cell2.Font = new Font("Times New Roman", 8.75f);
                    cell2.TextAlignment = TextAlignment.MiddleLeft;
                    row2.Cells.Add(cell);
                    row2.Cells.Add(cell2);
                    table.Rows.Add(row2);
                    table.HeightF = 15f;
                    table.WidthF = 164f;
                    table.TopF = num2;
                    table.LeftF = num;
                    Band.Controls.Add(table);
                    if (((Report.PageWidth - (Report.Margins.Left + Report.Margins.Right)) - num) >= 328f)
                    {
                        num += 164f;
                    }
                    else
                    {
                        num = 0f;
                        num2 += 18f;
                    }
                }
            }
            if (IsShowShift)
            {
                var label2 = new XRLabel
                {
                    Text = "K\x00fd hiệu chấm c\x00f4ng theo ca",
                    Font = new Font("Times New Roman", 9.25f, FontStyle.Italic | FontStyle.Bold),
                    TopF = num2 += 20f,
                    WidthF = 200f,
                    TextAlignment = TextAlignment.MiddleLeft
                };
                Band.Controls.Add(label2);
                num = 0f;
                num2 += 30f;
                var dic_shift = new DIC_SHIFT();
                foreach (DataRow row in dic_shift.GetList().Rows)
                {
                    str = "";
                    if (row["ShiftCode"].ToString() == "")
                    {
                        str = "(trống)";
                    }
                    else
                    {
                        str = row["ShiftCode"].ToString();
                    }
                    table = new XRTable();
                    row2 = new XRTableRow();
                    cell = new XRTableCell();
                    cell2 = new XRTableCell();
                    cell.WidthF = 136f;
                    cell.Text = row["ShiftName"] + " (" +
                                Convert.ToDateTime(row["BeginTime"].ToString()).ToShortTimeString() + " - " +
                                Convert.ToDateTime(row["EndTime"].ToString()).ToShortTimeString() + ")";
                    cell.WordWrap = false;
                    cell.Font = new Font("Times New Roman", 8.75f);
                    cell.TextAlignment = TextAlignment.MiddleLeft;
                    cell2.WidthF = 62f;
                    cell2.Text = ": " + str;
                    cell2.WordWrap = false;
                    cell2.Font = new Font("Times New Roman", 8.75f);
                    cell2.TextAlignment = TextAlignment.MiddleLeft;
                    row2.Cells.Add(cell);
                    row2.Cells.Add(cell2);
                    table.Rows.Add(row2);
                    table.HeightF = 15f;
                    table.WidthF = 198f;
                    table.TopF = num2;
                    table.LeftF = num;
                    Band.Controls.Add(table);
                    if (((Report.PageWidth - (Report.Margins.Left + Report.Margins.Right)) - num) >= 396f)
                    {
                        num += 198f;
                    }
                    else
                    {
                        num = 0f;
                        num2 += 18f;
                    }
                }
            }
        }

        public string Delete()
        {
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {m_SymbolCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_SYMBOL_Delete", myParams, myValues);
        }

        public string Delete(string SymbolCode)
        {
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {SymbolCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_SYMBOL_Delete", myParams, myValues);
        }

        public string Delete(SqlConnection myConnection, string SymbolCode)
        {
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {SymbolCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_SYMBOL_Delete", myParams, myValues);
        }

        public string Delete(SqlTransaction myTransaction, string SymbolCode)
        {
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {SymbolCode};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_SYMBOL_Delete", myParams, myValues);
        }

        public bool Exist(string SymbolCode)
        {
            bool hasRows = false;
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {SymbolCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_SYMBOL_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public string Get(string SymbolCode)
        {
            string str = "";
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {SymbolCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("DIC_SYMBOL_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_SymbolCode = Convert.ToString(reader["SymbolCode"]);
                    m_SymbolName = Convert.ToString(reader["SymbolName"]);
                    m_PercentSalary = Convert.ToDouble(reader["PercentSalary"]);
                    m_IsEdit = Convert.ToBoolean(reader["IsEdit"]);
                    m_IsShow = Convert.ToBoolean(reader["IsShow"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlConnection myConnection, string SymbolCode)
        {
            string str = "";
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {SymbolCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myConnection, "DIC_SYMBOL_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_SymbolCode = Convert.ToString(reader["SymbolCode"]);
                    m_SymbolName = Convert.ToString(reader["SymbolName"]);
                    m_PercentSalary = Convert.ToDouble(reader["PercentSalary"]);
                    m_IsEdit = Convert.ToBoolean(reader["IsEdit"]);
                    m_IsShow = Convert.ToBoolean(reader["IsShow"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public string Get(SqlTransaction myTransaction, string SymbolCode)
        {
            string str = "";
            string[] myParams = {"@SymbolCode"};
            object[] myValues = {SymbolCode};
            var helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader(myTransaction, "DIC_SYMBOL_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_SymbolCode = Convert.ToString(reader["SymbolCode"]);
                    m_SymbolName = Convert.ToString(reader["SymbolName"]);
                    m_PercentSalary = Convert.ToDouble(reader["PercentSalary"]);
                    m_IsEdit = Convert.ToBoolean(reader["IsEdit"]);
                    m_IsShow = Convert.ToBoolean(reader["IsShow"]);
                    m_Description = Convert.ToString(reader["Description"]);
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public DataTable GetList()
        {
            var helper = new SqlHelper();
            return helper.ExecuteDataTable("DIC_SYMBOL_GetList");
        }

        public string Insert()
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {m_SymbolCode, m_SymbolName, m_PercentSalary, m_IsEdit, m_IsShow, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_SYMBOL_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction)
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {m_SymbolCode, m_SymbolName, m_PercentSalary, m_IsEdit, m_IsShow, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_SYMBOL_Insert", myParams, myValues);
        }

        public string Insert(string SymbolCode, string SymbolName, double PercentSalary, bool IsEdit, bool IsShow,
            string Description)
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {SymbolCode, SymbolName, PercentSalary, IsEdit, IsShow, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_SYMBOL_Insert", myParams, myValues);
        }

        public string Insert(SqlConnection myConnection, string SymbolCode, string SymbolName, double PercentSalary,
            bool IsEdit, bool IsShow, string Description)
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {SymbolCode, SymbolName, PercentSalary, IsEdit, IsShow, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_SYMBOL_Insert", myParams, myValues);
        }

        public string Insert(SqlTransaction myTransaction, string SymbolCode, string SymbolName, double PercentSalary,
            bool IsEdit, bool IsShow, string Description)
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {SymbolCode, SymbolName, PercentSalary, IsEdit, IsShow, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_SYMBOL_Insert", myParams, myValues);
        }

        public string NewID()
        {
            return SqlHelper.GenCode("DIC_SYMBOL", "SymbolCode", "KH");
        }

        public string Update()
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {m_SymbolCode, m_SymbolName, m_PercentSalary, m_IsEdit, m_IsShow, m_Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_SYMBOL_Update", myParams, myValues);
        }

        public string Update(string SymbolCode, string SymbolName, double PercentSalary, bool IsEdit, bool IsShow,
            string Description)
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {SymbolCode, SymbolName, PercentSalary, IsEdit, IsShow, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery("DIC_SYMBOL_Update", myParams, myValues);
        }

        public string Update(SqlConnection myConnection, string SymbolCode, string SymbolName, double PercentSalary,
            bool IsEdit, bool IsShow, string Description)
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {SymbolCode, SymbolName, PercentSalary, IsEdit, IsShow, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "DIC_SYMBOL_Update", myParams, myValues);
        }

        public string Update(SqlTransaction myTransaction, string SymbolCode, string SymbolName, double PercentSalary,
            bool IsEdit, bool IsShow, string Description)
        {
            string[] myParams = {"@SymbolCode", "@SymbolName", "@PercentSalary", "@IsEdit", "@IsShow", "@Description"};
            object[] myValues = {SymbolCode, SymbolName, PercentSalary, IsEdit, IsShow, Description};
            var helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "DIC_SYMBOL_Update", myParams, myValues);
        }
    }
}